<template>
  <div class="app-container">
    <div class="Permissmain">
      <div class="from">
        <div class="from-input">
          <span>用户名称</span>
          <el-input placeholder="请输入用户名称" v-model="input2"> </el-input>
        </div>
        <el-button type="primary">查询</el-button>
        <el-button type="primary">重置</el-button>
      </div>
      <div class="btn">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="dialogFormVisible = true"
          v-on:click="getUserIsNotRoleList"
          >新增</el-button
        >
        <el-button type="danger" plain icon="el-icon-plus" @click="allcancel"
          >批量取消授权</el-button
        >
      </div>

      <el-table
        v-loading="listLoading"
        :data="matching"
        element-loading-text="Loading"
        fit
        highlight-current-row
        @select="sectionSelect"
      >
      <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="ID" width="118.5">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column label="员工姓名" width="237.5">
          <template slot-scope="scope">
            {{ scope.row.userName }}
          </template>
        </el-table-column>
        <el-table-column label="部门" width="237.5">
          <template slot-scope="scope">
            {{ scope.row.deptName }}
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="角色权限" width="237.5">
          <template slot-scope="scope">
            {{ scope.row.roleName }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="237.5">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="237.5">
          <template slot-scope="scope">
            <el-button
              class="el-icon-edit-outline"
              size="mini"
              v-on:click="Cancel(scope.row.id)"
              type="text"
              >取消授权</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title=" 选择用户" :visible.sync="dialogFormVisible">
      <el-table
        v-loading="listLoading"
        :data="tableData"
        element-loading-text="Loading"
        fit
        highlight-current-row
        @select="rowSelect"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="ID" width="118.5">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column label="员工姓名" width="132.5">
          <template slot-scope="scope">
            {{ scope.row.userName }}
          </template>
        </el-table-column>
        <el-table-column label="部门" width="132.5">
          <template slot-scope="scope">
            {{ scope.row.deptName }}
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="角色权限" width="132.5">
          <template slot-scope="scope">
            {{ scope.row.userName }}
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="132">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="dialogFormVisible = false"
          v-on:click="addPermiss"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { cancelPermission,getUserIsNotRoleList,allotUserPermission,getUserRoleList,cancelAllUserPermission } from "@/api/Permissions";
export default {
  data() {
    return {
      //选择用户的id
      chooseId:[],
      chooseId1:[],
      //批量取消id
      allChooseId:[],
      allChooseId1:[],
      //权限ID
      roleId:"",      
      //未匹配
      tableData: [],
      dialogFormVisible: false,
      list: [ ],
      form: {
        name: "",
        email: "",
        phone: "",
        address: "",
        age: "",
        role: "",
      },
      listLoading: false,
      input2: "",
      input3: "",
      matching: [],
      
    };
  },
  created() {
    // this.matching = JSON.parse(localStorage.getItem("userRole"));
    // console.log(this.matching);
    this.matchings();
    this.getRoleId();
  },
  methods: {
    //分配权限
    addPermiss() {
      this.chooseId.forEach(item=>{
        this.chooseId1.push(item.id)
      })
      let obj = {
        roleId:this.roleId,
        userIds:this.chooseId1
      }
      allotUserPermission(obj).then(res=>{
        this.getUserRoleList()
        this.$message({
          message: "分配成功",
          type: "success",
        });
      })
    },
    //获取批量取消id
    sectionSelect(selection){
      this.allChooseId = selection
      console.log(this.allChooseId,"22222")

    },
    //批量取消权限
    allcancel(){
      this.allChooseId.forEach(item=>{
        this.allChooseId1.push(item.id)
      })
      console.log(this.allChooseId1,"31332")
    let obj = {
      userIds:this.allChooseId1
    }
    cancelAllUserPermission(obj).then(res=>{
      this.getUserRoleList()
      this.$message({
          message: "批量取消成功",
          type: "success",
        });
    })

    },
    fetchData() {},
    useradd() {},
    Cancel(id) {
      let obj = {
        userId: id,
      };
      cancelPermission(obj).then((res) => {
        // this.matchings();
        this.getUserRoleList();
        this.$message({
          message: "取消成功",
          type: "success",
        });
      });
      this.matching.forEach((item, index) => {
        if (item.id == id) {
          this.matching.splice(index, 1);
        }
      });
      console.log(this.matching);
      localStorage.setItem("userRole", JSON.stringify(this.matching));
    },
  async  matchings() {
      this.matching = await JSON.parse(localStorage.getItem("userRole"));
      // console.log(this.matching,"2222")
    },
  async  getRoleId() {
      this.roleId = await JSON.parse(localStorage.getItem("roleId"));
      // console.log(this.roleId,"5555")
    },
    //获取未匹配权限用户
    getUserIsNotRoleList(){
      let obj = {
        roleId:this.roleId,
        page:1,
        limit:10,
      }
      console.log(obj)
      getUserIsNotRoleList(obj).then(res=>{
        this.tableData = res.data

      })

    },
     //获取已匹配权限用户
   async getUserRoleList(){
      let obj = {
        roleId:this.roleId,
        page:1,
        limit:20
      }
     await getUserRoleList(obj).then(res=>{
        this.matching = res.data
       
      })

    },

    //获取选择用户的id
    rowSelect(selection){
      this.chooseId = selection
      console.log(this.chooseId)
    }  
    },
};
</script>
<style scoped>

.employee-header {
  display: flex;
  padding: 0;
}
.employee-header .el-input {
  width: 200px;
}
.employee-header .el-button {
  height: 40px;
  margin-left: 10px;
}
.from-input .el-input {
  margin: 0 10px;
}
.from {
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 20px;
}
.Permissmain .el-input {
  width: 200px;
  height: 40px;
}
.el-table_1_column_1 {
  text-align: center !important;
}
.el-table_1_column_2 {
  text-align: center !important;
}
.el-table_1_column_3 {
  text-align: center !important;
}
.el-table_1_column_4 {
  text-align: center !important;
}
.el-table_1_column_5 {
  text-align: center !important;
}
.el-table_1_column_6 {
  display: flex !important;
  justify-content: center !important;
}
</style>
<style lang="scss">
.Permissmain{
  .el-table td,tr{
    text-align: center !important;
  }
 .el-table td, .el-table th.is-leaf {
  text-align: center !important;
 }
}
.cell{
  text-align: center;
}

</style>